# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
习题一：
1.定义一个天气温度列表，写出里面每一天的温度
2.打印一周的天气，周三打印例外为：周三+温度
习题二：
1.定义一个字典，存储5天的天气信息，温度、天气情况
2.打印星期三的温度和天气情况
weather={'周一':['23','良好'],
         '周二':['22','良好'],
         '周三':['24','阴天'],
         '周四':['25','良好'],
         '周五':['28','高温'],
         '周六':['27','良好'],
         '周日':['30','高温预警']}
print(weather['周三'])
"""
"""
Python3 的六个标准数据类型中：
    不可变数据（3 个）：Number（数字）、String（字符串）、Tuple（元组）；
    可变数据（3 个）：List（列表）、Dictionary（字典）、Set（集合）。
"""

"""
加载天气数据
解析字典
urllib.quote(data)
import urllib.request as r
url = 'https://api.shenjian.io/weather/area?appid=80ac6de09bbf051de598cf4081b44216&area=%E6%9D%AD%E5%B7%9E&only_now=true'
#url = 'http://api.openweathermap.org/data/2.5/weather?q=shanghai&mode=json&units=metric&lang=zh_cn&APPID=6a67ed641c0fda8b69715c43518b6996'
data = r.urlopen(url).read().decode('utf-8','ignore')
print(data)
import json
data = json.loads(data)
print('地区：'+data['name'])
print('天气情况：'+data['weather'][0]['description'])
print('温度：'+str(data['main']['temp']))
print('气压：'+str(data['main']['pressure']))
"""
"""
#利用神箭手天气api查询天气
import urllib.request as r
import json
a=r.quote('杭州')
url = 'https://api.shenjian.io/weather/area?appid=80ac6de09bbf051de598cf4081b44216&' + 'area='  +a+ '&only_now=false'
data = r.urlopen(url).read().decode('utf-8','ignore')
data = json.loads(data)
print(data)
"""
"""
URL地址拼接
URL = "http://127.0.0.1/version"
dicts = {'platform': 'windows_64bit', 'object': 'agent', 'version': '20160216'}
os_type={"os_type":"win_xp"}
def parse_url(data={}):
    item=data.items()
    urls="?"
    for i in item:
        (key,value)=i
        temp_str = key+"="+value
        urls = urls + temp_str+"&"
    urls = urls[:len(urls)-1]

"""
"""
练习题4：
1.打印每天18点的天气信息，温度，情况，气压，最高温度，最低温度
2.写出英文版的天气-天气情况，用户输入英文   application应用
3.打印温度折线图
 item['weather']['description']
 print(item['weather'][0]['description'])
    1----------
    2--------------------
    3-------
    4----------
4.获取所有的温度，并且排序（sorted([1,4,-1,8])##########使用此方法排序）
5.友情提示，根据温度提示穿衣，打伞，出门(可选)
"""
"""
import json
import urllib.request as r
place = input("请输入城市（拼音）：")
#place = 'chongqing'
url = 'http://api.openweathermap.org/data/2.5/forecast?q='+place+',cn&mode=json&lang=zh_cn&&APPID=6a67ed641c0fda8b69715c43518b6996&units=metric'
data = r.urlopen(url).read().decode('utf-8','ignore')
data = json.loads(data)
temp = {}#定义temp字典存放温度，用于打印折线图
list = data['list']#获取天气情况
#打印天气情况函数---参数：气温，最低气温，最高气温，气压，天气情况
def printWeather(temper,min_temper,max_temper,pressure,description):
    print('温度：'+str(temper),'最低温度：'+str(min_temper),'最高温度：'+str(max_temper),'气压：'+str(pressure),'情况：'+str(description))
for item in list:
    if item['dt_txt'].endswith('18:00:00'):
#    if item['dt_txt']:
        print(item['dt_txt'])#打印时间
        temp[ (item['dt_txt']) ] = item['main']['temp']#将温度放入temp字典
        printWeather(item['main']['temp'],item['main']['temp_min'],item['main']['temp_max'],item['main']['pressure'],item['weather'][0]['description'])

tempList = temp.values()
#优化温差计算值，提高折线图识别率
def printTemp(temp,min_temp,leavel):
    return int((temp - min_temp + 1)*leavel)
cha = 10 / (max(tempList) - min(tempList))
for temps in temp:
    print(temps,'-'* printTemp( temp[(temps)] , min(tempList) , cha) )
"""

"""
显示4个商品后加分割线
36个商品
获取所有价格并高到低排序
商品过滤只显示15天退款或包邮的产品信息
"""
"""
import json
import urllib.request as r
quaryText = r.quote('连衣裙') 
url = 'https://s.taobao.com/search?q='+quaryText+'&imgfile=&commend=all&ssid=s5-e&search_type=item&sourceId=tb.index&spm=a21bo.2017.201856-taobao-item.1&ie=utf8&initiative_id=tbindexz_20170306&ajax=true'
data = r.urlopen(url).read().decode('utf-8','ignore')
data = json.loads(data)
itemList = data['mods']['itemlist']['data']['auctions']
goodsList = []
for item in itemList:
    goodsList.append({
        "商品标题":item['raw_title'],
        "商品价格":float(item['view_price']),
        "购买人数":int(item['view_sales'][:-3]),
#        "头图地址":item['pic_url'],
#       "详情页":item['detail_url'],
#        "卖家地址":item['item_loc'],
        "是否包邮":True if float(item['view_fee'])==0 else False,
        "邮费":float(item['view_fee'])
        })
#对商品价格进行降序放于goodsListPrice
goodsListPrice = sorted(goodsList,key = lambda i:i['商品价格'],reverse = True)
#对购买人数升序放于goodsListSales
goodsListSales = sorted(goodsList,key = lambda i:i['购买人数'])
def printInfo(ary):
    for index in range(len(ary)):
        if(index%4 == 0):
            print('*********************************************')
        print(ary[index])
priceList = []
salesList = []
printInfo(goodsListPrice)
for index in range(len(goodsListPrice)):
    priceList.append(goodsListPrice[index]['商品价格'])
    salesList.append(goodsListSales[index]['购买人数'])
print('按照价格高至低排序：')
print(priceList)
print('按照购买人数低至高排序：')
print(salesList)
"""
"""
        


练习8：保存淘宝数据（小组项目）
1.爬100页数据
2.保存淘宝商品信息，并保存csv格式
3.组长合并数据
https://s.taobao.com/search?q=%E8%A3%99%E5%AD%90&imgfile=&commend=all&ssid=s5-e&search_type=item&sourceId=tb.index&spm=a21bo.2017.201856-taobao-item.1&ie=utf8&initiative_id=tbindexz_20170306&loc=%E6%B1%95%E5%A4%B4
word = '裙子'
pages = 0+44*1
rul = 'https://s.taobao.com/search?q='+word+'&imgfile=&commend=all&ssid=s5-e&search_type=item&sourceId=tb.index&spm=a21bo.2017.201856-taobao-item.1&ie=utf8&initiative_id=tbindexz_20170306&ajax=true&s='+pages
"""    
    
import json
import urllib.request as r
import time
word = r.quote('裙子')
loc = r.quote('汕头')
url = 'https://s.taobao.com/search?q='+word+'&imgfile=&commend=all&ssid=s5-e&search_type=item&sourceId=tb.index&spm=a21bo.2017.201856-taobao-item.1&ie=utf8&initiative_id=tbindexz_20170306&loc='+ loc +'&ajax=true'
itemList = []
def reqTimeOut(url,timeOut):
#    fileURL = open('reqUrl.txt','a+',encoding='utf-8')
    fileList = open('reqList1.txt','a+',encoding='gbk')
    for i in range(0,100):
        urlPage = url + '&s=' + str(i*44)
        f= r.urlopen(urlPage)
        if f.getcode() == 200 :
            data = f.read().decode('utf-8','ignore')
        else:
            print('网络请求错误,正在重试中。。。')
            i = i-1
        data = json.loads(data)
        data = json.dumps(data['mods']['itemlist']['data']['auctions'])
#        try:
#            fileURL.write(urlPage+'\n')
#        except Exception as err:
#            print('文件写入URL错误！重试中。。。')
#            continue
        try:
            fileList.write(data + '\n')
        except Exception as err:
            print('文件写入Data错误！重试中。。。')
            continue
#        itemList.append(data['mods']['itemlist']['data']['auctions'])
        print('爬取第'+str(i+1)+'页成功')
        time.sleep(timeOut)
#    fileURL.close()
    fileList.close()
reqTimeOut(url,0)
















